<?php

/**
 * @author: biggege
 * @since:  2021-08-05 17:04:22
 */
namespace App\Helpers;

use Symfony\Component\HttpFoundation\Response as FoundationResponse;
use Response;

trait ManageResponse
{

    /**
     * [$statusCode description]
     * @var [type]
     */
    protected $statusCode = FoundationResponse::HTTP_OK;

    /**
     * [getStatusCode description]
     * @return mixed
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }

    /**
     * [setStatusCode description]
     * @param  [type] $statusCode [description]
     * @return $this
     */
    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;
        return $this;
    }

    /**
     * [respond description]
     * @param  [type] $data   [description]
     * @param  array  $header [description]
     * @return mixed
     */
    public function respond($data, $header = [])
    {
        $resp = Response::json($data, 200, $header);//创建，更新，删除，查询，都返回200 statusCode
        return $resp;
    }

    /**
     * [status description]
     * @param  [type]  $message [description]
     * @param  array   $data    [description]
     * @param  [type]  $code    [description]
     * @return mixed
     */
    public function status($message, array $data=[], $code = null)
    {
        if ($code) {
            $this->setStatusCode($code);
        }

        $status = [
            'code' => $this->statusCode,
            'msg'  => $message
        ];

        $data = array_merge($status, $data);

        return $this->respond($data);
    }

    /**
     * [failed description]
     * @param  string $message [description]
     * @param  [type] $code    [description]
     * @return [type]          [description]
     */
    public function failed($message = '操作失败', $code = FoundationResponse::HTTP_BAD_REQUEST)
    {
        return $this->setStatusCode($code)->status($message, ['data' => []], null);
    }

    /**
     * [redirected description]
     * @param  string $status  [description]
     * @param  string $message [description]
     * @param  string $url     [description]
     * @return [type]          [description]
     */
    public function redirected($status = 'success', $message = '操作成功', $url = '/')
    {
        session()->put($status, $message);
        return redirect($url);
    }

    /**
     * [message description]
     * @param  string $message [description]
     * @return [type]          [description]
     */
    public function message($message = "操作成功")
    {
        return $this->status($message, []);
    }

    /**
     * [success description]
     * @param  array   $data    [description]
     * @param  string  $message [description]
     * @return [type]           [description]
     */
    public function success($data = [], $message = "操作成功")
    {
        if($this->arrayDepth($data) == 1 && !empty($data)) {
            $data = [$data];
        }
        return $this->status($message, compact('data'), null);
    }

    /**
     * [notFond description]
     * @param  string $message [description]
     * @return [type]          [description]
     */
    public function notFond($message = '未发现')
    {
        return $this->failed($message, Foundationresponse::HTTP_NOT_FOUND);
    }

    /**
     * [arrayDepth description]
     * @param  [type] $array [description]
     * @return [type]        [description]
     */
    public static function arrayDepth($array) 
    {
        if(!is_array($array)) return 0;
        $max_depth = 1;
        foreach ($array as $value) {
            if (is_array($value)) {
                $depth = self::arrayDepth($value) + 1;
                
                if ($depth > $max_depth) {
                    $max_depth = $depth;
                }
            }
        }
        return $max_depth;
    }

}